package com.trkj.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.DeptDAO;
import com.trkj.dao.EmpDAO;
import com.trkj.dao.EmployDAO;
import com.trkj.dao.PositionDAO;
import com.trkj.pojo.Dept;
import com.trkj.pojo.Emp;
import com.trkj.pojo.Employ;
import com.trkj.pojo.Position;


@Service
@Transactional
public class EmploysService {

	@Autowired
	EmpDAO empDAO;
	@Autowired
	DeptDAO deptDAO;
	@Autowired
	PositionDAO positionDAO;
	@Autowired
	EmployDAO employDAO;
	@Autowired
	WorkFlowService workFlowService;
	@Autowired
	TaskService taskService;
	/**
	 * 查询任务名称
	 * @author Lixd
	 * @param taskId
	 * @return
	 */
	public String now_task_name(String taskId) {
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		return task.getName();
	}
	/**
	 * 查询所有员工
	 * @author Lixd
	 * @return
	 */
	public List<Emp> findall(){
	 	return empDAO.findAll();
	}
	
	/**
	 * 查询所有部门的信息，id和name封装到map
	 * @author Lixd
	 * @return
	 */
	public List<Map<String, Object>> findAll_dept_combobox() {
		List<Dept> findAll	= deptDAO.findAll();
		List<Map<String, Object>> listmap = new ArrayList<>();
		if (findAll!=null) {
			for (Dept dept : findAll) {
				Map<String, Object> map = new HashMap<>();
				map.put("deptId", dept.getDeptId());
				map.put("deptName", dept.getDeptName());
				listmap.add(map);
			}
		}
		return listmap;
	}
	/**
	 * 查询所有职位的信息，id和name封装到map
	 * @author Lixd
	 * @return
	 */
	public List<Map<String, Object>> findAll_position_combobox(Integer deptId) {
		Dept dept = deptDAO.findOne(deptId);
		List<Position> findAll = dept.getPositions();
		List<Map<String, Object>> listmap = new ArrayList<>();
		if (findAll!=null) {
			for (Position position : findAll) {
				Map<String, Object> map = new HashMap<>();
				map.put("positionId", position.getPositionId());
				map.put("positionName", position.getPositionName());
				listmap.add(map);
			}
		}
		return listmap;
	}
	/**
	 * 保存流程
	 * @author Lixd
	 * @param employ
	 * @param deptId
	 * @param positionId
	 * @return
	 */
	public Integer save_employs(Integer useid ,Employ employ,Integer deptId,Integer positionId) {
		try {
			if (employ!=null) {
				if (deptId!=null) {
					Dept dept = deptDAO.findOne(deptId);
					employ.setDept(dept);
				}
				if (positionId!=null) {
					Position position = positionDAO.findOne(positionId);
					position.setPositionNumber(position.getPositionNumber()+1);
					employ.setPosition(position);
				}
				employ.setLunchPeople(useid);
				return employDAO.save(employ).getEmployId();
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
}
